'use strict';

exports.__esModule = true;

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /* flow */


var _graphql = require('graphql');

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
 * Abstract base class to create GraphQLScalars.
 * @abstract
 */
var Base = function () {

  /**
   * Constructs a builder with a name for the GraphQLScalar.
   * @example const GraphQLStringBuilder = GraphQLScalarTypes.string('Name');
   * @example const GraphQLNumberBuilder = GraphQLScalarTypes.number('Age');
   * @example const GraphQLBooleanBuilder = GraphQLScalarTypes.boolean('SpecialFalsy');
   */
  function Base(name) {
    _classCallCheck(this, Base);

    if (!name) {
      throw new Error('Type must be named');
    }
    (0, _graphql.assertValidName)(name);
    this.name = name;
    this._func = [];
  }

  /**
   * Gives a description to the GraphQLScalar.
   */


  _createClass(Base, [{
    key: 'describe',
    value: function describe(description) {
      if (typeof description !== 'string') {
        throw new TypeError('description must be a string');
      }
      var obj = this.clone();
      obj.description = description;
      return obj;
    }

    /**
     * Makes the GraphQLScalar reject `null` as a value.
     * @param {boolean} [isNonNull=true] Default value is true.
     */

  }, {
    key: 'nonNull',
    value: function nonNull() {
      var isNonNull = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;

      var obj = this.clone();
      obj._isNonNull = isNonNull;
      return obj;
    }

    /**
     * Clones the builder.
     */

  }, {
    key: 'clone',
    value: function clone() {
      var obj = Object.assign(Object.create(this), this);
      obj._func = obj._func.slice();
      return obj;
    }

    /**
     * Generates the GraphQLScalar.
     */

  }, {
    key: 'create',
    value: function create() {
      var scalar = new _graphql.GraphQLScalarType({
        name: this.name,
        description: this.description,
        serialize: this.serialize,
        parseValue: this.parseValue,
        parseLiteral: this.parseLiteral
      });
      return this._isNonNull ? new _graphql.GraphQLNonNull(scalar) : scalar;
    }
  }]);

  return Base;
}();

exports.default = Base;